/*
 * Copyright (C) 2007 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.welchapps.nporganizer;


import java.util.ArrayList;

import com.welchapps.nporganizer.NotePad.DataCount;

import android.app.ActionBar;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Spinner;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;



public class FilterList extends Activity implements OnItemSelectedListener {

    /**
     * Standard projection for the interesting columns of a normal note.
     */
	private Context context = this;
    private AutoCompleteTextView mValue;
    private Spinner cat, status;
    ArrayAdapter<CharSequence> adapter_cat, adapter_stat;

    /**
     * A custom EditText that draws lines between each line of text that is displayed.
     */


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Set the layout for this activity.  You can find it in res/layout/note_editor.xml
        setContentView(R.layout.filter_list);
        //final Intent intent = getIntent();

        ActionBar actionBar = getActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);

        cat = (Spinner) this.findViewById(R.id.cat_spin); 




        status = (Spinner) this.findViewById(R.id.status_spin);  
        // The text view for our note, identified by its ID in the XML file.
        mValue = (AutoCompleteTextView) findViewById(R.id.value);
        // Create an ArrayAdapter using the string array and a default spinner layout
        adapter_cat = ArrayAdapter.createFromResource(this,
                R.array.filter_catlist, android.R.layout.simple_spinner_item);
        // Specify the layout to use when the list of choices appears
        adapter_cat.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        // Create an ArrayAdapter using the string array and a default spinner layout
        adapter_stat = ArrayAdapter.createFromResource(this,
                R.array.filter_status_array, android.R.layout.simple_spinner_item);
        // Specify the layout to use when the list of choices appears
        adapter_stat.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        // Apply the adapter to the spinner
        cat.setAdapter(adapter_cat);
        status.setAdapter(adapter_stat);
        
        SharedPreferences settings = getSharedPreferences("filterPref",0);
        mValue.setText(settings.getString("value",""));
        cat.setOnItemSelectedListener(this);
        cat.setSelection(adapter_cat.getPosition(settings.getString("category","None")));
        status.setSelection(adapter_stat.getPosition(settings.getString("status","Both")));
        
        

    }

    @Override
    protected void onResume() {
        super.onResume();
        // If we didn't have any trouble retrieving the data, it is now
        // time to get at the stuff.

    }



    @Override
    protected void onPause() {
        super.onPause();

    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate menu from XML resource
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.filter_options_menu, menu);

        return super.onCreateOptionsMenu(menu);
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case android.R.id.home:
        	okFilter(null);
        	return true;
        case R.id.filter_ok:
            // Launch activity to insert a new item
            okFilter(null);
            return true;
        case R.id.filter_revert:
        	clearFilter(null);
            return true;    
        default:
            return super.onOptionsItemSelected(item);
        }
    }

    @Override
	public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {		
    	ArrayList<String> list = new ArrayList<String>();
    	ArrayAdapter<String> aadapter;
		switch((int)arg3){
		case 1:
			list = DataCount.BRAND;
			break;
		case 2:
			list = DataCount.COLLECTION;
			break;
		case 3:
			list = DataCount.COLOR;
			break;
		case 4:
			list = DataCount.FINISH;
			break;
		default:
			list.clear();
			break;
		}
        String[] alist = new String[list.size()];
        alist = list.toArray(alist);
        aadapter = new ArrayAdapter<String> (context, android.R.layout.simple_list_item_1, alist);
        
        mValue.setAdapter(aadapter);
	}
	
	@Override
	public void onNothingSelected(AdapterView<?> arg0) {
		// TODO Auto-generated method stub
		
	}
    
    public void okFilter(View view){
    	SharedPreferences settings = getSharedPreferences("filterPref",0);
    	SharedPreferences.Editor editor = settings.edit();
    	
    	editor.putString("category",cat.getSelectedItem().toString());
    	editor.putString("status",status.getSelectedItem().toString());
    	editor.putString("value",mValue.getText().toString().trim());
    	editor.commit();
    	finish();
    }
    
    public void clearFilter(View view){
        mValue.setText("");
        cat.setSelection(adapter_cat.getPosition("None"));    	
        status.setSelection(adapter_stat.getPosition("Both"));   
    }


 

}
